package com.vancouver.knows.ui.fragment.event;

import java.util.List;

import android.animation.Animator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import butterknife.Bind;
import butterknife.OnClick;

import com.astuetz.PagerSlidingTabStrip;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.browser.LoadableList;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.MainApplication.TrackerName;
import com.vancouver.knows.business.bean.UserInfo;
import com.vancouver.knows.ui.activity.event.EventCalendarActivity;
import com.vancouver.knows.ui.activity.search.NewSearchActivity;
import com.vancouver.knows.ui.activity.search.SearchActivity;
import com.vancouver.knows.ui.base.BaseFragment;
import com.vancouver.knows.ui.base.LoadingFragment;
import com.vancouver.knows.ui.common.MyAutoCompleteTextView;
import com.vancouver.knows.ui.view.compactcalendarview.AnimatorListener;
import com.vancouver.knows.util.AnalyticsConstants;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.util.PhoConstants;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.business.bean.EventBannerInfo;

/**
 * 活动
 * 
 * @author chenxunlin01
 * 
 */
public class EventFragment extends LoadingFragment implements LoadableList.LoadableListScrollListener {

	@Bind(R.id.tabStrip)
	PagerSlidingTabStrip indicator;
	@Bind(R.id.pager)
	ViewPager viewPager;
	@Bind(R.id.toolbar)
	Toolbar mToolbar;
	@Bind(R.id.scroll_shadow_left)
	ImageView scrollShadowLeft;
	@Bind(R.id.scroll_shadow_right)
	ImageView scrollShadowRight;

	SparseArray<Fragment> fragmentMap = new SparseArray<Fragment>();
	private String[] titles;
	private int pageNum;
	private EventPagerAdapter pagerAdapter;

	public static EventFragment newInstance() {
		return new EventFragment();
	}

	@Override
	public void onResume() {
		super.onResume();

		Tracker t = ((MainApplication) getActivity().getApplication()).getTracker(TrackerName.APP_TRACKER);
		t.setScreenName(AnalyticsConstants.CUSTOM_EVENT_VALUE_EVENT_FRAGMENT);
		t.send(new HitBuilders.ScreenViewBuilder().build());
	}

	@Override
	protected void bindView(View rootView) {
		titles = getResources().getStringArray(R.array.event_tab_titles);
		pageNum = titles.length;
		pagerAdapter = new EventPagerAdapter(getChildFragmentManager());
		viewPager.setAdapter(pagerAdapter);
		indicator.setViewPager(viewPager);

		// setup scroll shadows for the pager title strip
		scrollShadowLeft.setVisibility(View.GONE);
		scrollShadowRight.setVisibility(View.VISIBLE);
		indicator.setPagerScrollListener(new PagerSlidingTabStrip.PagerScrollListener() {
			@Override
			public void onScrollChanged(PagerSlidingTabStrip view, int x, int y, int oldx, int oldy) {

				View lastView = view.getChildAt(view.getChildCount() - 1);
				int diff = lastView.getRight() - (view.getWidth() + view.getScrollX() + lastView.getLeft());
				if (diff <= 0) {
					// reached end of pager title strip
					scrollShadowLeft.setVisibility(View.VISIBLE);
					scrollShadowRight.setVisibility(View.GONE);
				} else if (view.getScrollX() <= 0) {
					// reached beginning of pager title strip
					scrollShadowLeft.setVisibility(View.GONE);
					scrollShadowRight.setVisibility(View.VISIBLE);
				} else {
					scrollShadowLeft.setVisibility(View.VISIBLE);
					scrollShadowRight.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	@OnClick(R.id.search_icon)
	public void onSearchIconClick() {
		Intent searchIntent = new Intent(getActivity(), NewSearchActivity.class);
		startActivity(searchIntent);
	}

	@OnClick(R.id.calendar_icon)
	public void onCalendarIconClick() {
		Intent intent = new Intent(getActivity(), EventCalendarActivity.class);
		startActivity(intent);
	}

	@Override
	public void subHandleMessage(Message msg) {
		switch (msg.what) {
		case Constants.BUSINESS_STATUS_EVENT_LIST_HEAD_POSTS_CHANGE:
			// no-op
			break;
		}
	}

	@Override
	protected ATaskMark getTaskMark() {
		return bModule.getTaskMarkPool().getFetchEventsTaskMark();
	}

	@Override
	protected void loadData() {
		String userId = "";
		UserInfo userInfo = bModule.getCacheManager().getUserInfo();
		if (userInfo != null) {
			userId = userInfo.getUserID();
		}
		bModule.getServiceWrapper().fetchEvents(this, getTaskMark(), userId, AppUtils.getPdg());
	}

	@Override
	protected View createContentView(LayoutInflater inflater) {
		return inflater.inflate(R.layout.activity_event, null, false);
	}

	public void scrollUp() {
		int currentIndex = viewPager.getCurrentItem();
		EventChildrenFragment eventChildrenFragment = (EventChildrenFragment) pagerAdapter.getItem(currentIndex);
		eventChildrenFragment.scrollUp();
	}

	@Override
	public void onScrollUp() {
		ViewPropertyAnimator viewPropertyAnimator = mToolbar.animate();
		viewPropertyAnimator.setListener(new AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {
				mToolbar.setVisibility(View.VISIBLE);
			}
		});
		viewPropertyAnimator.translationY(0).setInterpolator(new AccelerateInterpolator()).start();
	}

	@Override
	public void onScrollDown() {
		ViewPropertyAnimator viewPropertyAnimator = mToolbar.animate();
		viewPropertyAnimator.setListener(new AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {
				mToolbar.setVisibility(View.GONE);
			}
		});
		viewPropertyAnimator.translationY(-mToolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
	}

	public class EventPagerAdapter extends FragmentStatePagerAdapter {

		public EventPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			Fragment fragment = (Fragment) super.instantiateItem(container, position);
			Fragment mFragment = fragmentMap.get(position);
			if (mFragment == null) {
				fragmentMap.put(position, fragment);
			}
			return fragment;
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = fragmentMap.get(position);
			if (fragment == null) {
				fragment = createFragment(position);
				fragmentMap.put(position, fragment);
			}
			return fragment;
		}


		@Override
		public int getCount() {
			return pageNum;
		}

		private Fragment createFragment(int position) {
			return EventChildrenFragment.newInstance(titles[position]);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return titles[position];
		}
	}

}
